package com.adminpanel.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.junit.Test;

public class Util {
	
	//取得当前时间值，格式是年月日时
	public String getDateTime(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");  
		Date datetime = new Date();  
		String c_datetime = sdf.format(datetime);
		return c_datetime;
		
	}
	
	public String getDate(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
		Date datetime = new Date();  
		String c_datetime = sdf.format(datetime);
		return c_datetime;
		
	}
	
	public String getTime(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date datetime = new Date();
		String log_datetime = sdf.format(datetime);
		return log_datetime;
	}
	
	//将指定含日期格式的指定字符串转换成long值以方便计算
	public long c_timeToSecond(String d_str){
		long ct = 0;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");  
		try {
			Date d  = sdf.parse(d_str);
			ct = d.getTime();
		} catch (ParseException e) {
			
			e.printStackTrace();
		}
		return ct;
	}
	
	//将当前时间值转为long值
	public long getCurrentTimeToSecond(){
		
		long cts = 0;
		cts = c_timeToSecond(getDateTime());
		return cts;
	}
	
	//将时间值base64
	public String encode64_time(String current_time){
		return Base64.encodeBase64String(current_time.getBytes());
		//return b_time;
	}
	
	public String  decode64_time(String b64str){
		String d_time = null;
		d_time = new String(Base64.decodeBase64(b64str));
		//d_time = Integer.valueOf(cc).intValue();//整形值
		return d_time;
	}
	
	
	public ArrayList<String> getRssContent(String url) {
		

		SAXReader reader = new SAXReader();
		Document doc = null;
		try {
			doc = reader.read(url);
		} catch (DocumentException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Node root = doc.selectSingleNode("/rss");
		List list = root.selectNodes("channel/item");
		ArrayList<String> al = new ArrayList<String>();
		for (Object o : list) {

			Element e = (Element) o;

			// Element show= e.element("title");

			al.add(e.element("title").getText());
			al.add(e.element("link").getText());
			// String aaa = show.getText();

		}
		return al;

	
	}
	

	public String getRss(String url){
		  HttpClient httpClient = new HttpClient();
	        GetMethod getMethod = new GetMethod(url);
	        getMethod.getParams().setContentCharset("UTF-8");
	        String response = null;
	        try {
	            int statusCode = httpClient.executeMethod(getMethod);
	            if (statusCode != org.apache.commons.httpclient.HttpStatus.SC_OK) {
	                System.err.println("Method failed: "+ getMethod.getStatusLine());
	            }
	            // 读取内容
	            //byte[] responseBody = getMethod.getResponseBody();
	            // 处理内容

	             //html = new String(responseBody);
				
	             response=new String(getMethod.getResponseBodyAsString().getBytes("UTF-8"));
	             //System.out.print(response);
	        } catch (Exception e) {
	            System.err.println("页面无法访问");
	        }
	        getMethod.releaseConnection();
			return response;
	}
		
		
		
	
	
	@Test
	public void testbase64(){
		System.out.println(encode64_time("20110908142001"));
	}
	
	public  String formatSize(float size)  

    {  

        long kb = 1024;  

        long mb = (kb * 1024);  

        long gb  = (mb * 1024);  

        if (size < kb) {  

            return String.format("%d B", (int) size);  

        }  

        else if (size < mb) {  

            return String.format("%.2f KB", size / kb); //保留两位小数  

        }  

        else if (size < gb) {  

            return String.format("%.2f MB", size / mb);  

        }  

        else {  

            return String.format("%.2f GB", size / gb);  

        }  

    }  


	
	
}
